package org.xxpay.pay.channel.ymkjpay;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;
import org.xxpay.core.common.constant.MchConstant;
import org.xxpay.core.common.constant.PayConstant;
import org.xxpay.core.common.util.MD5Util;
import org.xxpay.core.common.util.MyLog;
import org.xxpay.core.entity.PayOrder;
import org.xxpay.pay.channel.BasePayNotify;

import javax.servlet.http.HttpServletRequest;

@Service
public class YmkjpayPayNotifyService extends BasePayNotify {

    private static final MyLog _log = MyLog.getLog(YmkjpayPayNotifyService.class);

    @Override
    public String getChannelName() {
        return YmkjpayConfig.CHANNEL_NAME;
    }

    @Override
    public JSONObject doNotify(Object notifyData) {
        String logPrefix = "【处理优术支付回调】";
        _log.info("====== 开始处理优术支付回调通知 ======");
        HttpServletRequest request = (HttpServletRequest) notifyData;
        JSONObject retObj = buildRetObj();
        PayOrder payOrder;
        String respString = YmkjpayConfig.RETURN_VALUE_FAIL;
        try {
            String order_id = request.getParameter("order_id");         // 流水号
            String out_order_id = request.getParameter("out_order_id"); // 商户订单号
            String price = request.getParameter("price");               // 支付金额
            String realprice = request.getParameter("realprice");       // 实际支付金额
            String type = request.getParameter("type");                 // 支付类型
            String paytime = request.getParameter("paytime");           // 支付时间
            String extend = request.getParameter("extend");             // 附加数据
            String sign = request.getParameter("sign");                 // 签名
            _log.info("{}请求参数：order_id={}, out_order_id={}, price={}, realprice={}, type={}, paytime={}, extend={}, sign={}",
                   logPrefix, order_id, out_order_id, price, realprice, type, paytime, extend, sign);

            payOrder = commonService.payOrderService.findByPayOrderId( request.getParameter("out_order_id"));
            if(payOrder == null) {
                respString = "订单不存在";
                retObj.put(PayConstant.RESPONSE_RESULT, respString);
                return retObj;
            }
            YmkjpayConfig ymkjPayConfig = new YmkjpayConfig(getPayParam(payOrder));
            String signTemp = "order_id="+order_id+"&out_order_id="+out_order_id+"&paytime="+paytime+"&price="+price+"&realprice="+realprice+"&type="+type+"&key="+ymkjPayConfig.getKey();
            if("".equals(paytime) || "0".equals(paytime)) {
                signTemp = "order_id="+order_id+"&out_order_id="+out_order_id+"&price="+price+"&realprice="+realprice+"&type="+type+"&key="+ymkjPayConfig.getKey();
            }

            _log.info("{}签名串：{}", logPrefix, signTemp);
            String mySign = MD5Util.string2MD5(signTemp).toUpperCase();
            //验签
            if(!mySign.equals(sign)){
                respString = "fail(verify fail)";
                retObj.put(PayConstant.RESPONSE_RESULT, respString);
                return retObj;
            }
            _log.info("{}签名验证通过");
            //处理业务
            byte payStatus = payOrder.getStatus(); // 0：订单生成，1：支付中，-1：支付失败，2：支付成功，3：业务处理完成，-2：订单过期
            if (payStatus != PayConstant.PAY_STATUS_SUCCESS && payStatus != PayConstant.PAY_STATUS_COMPLETE) {
                int updatePayOrderRows = commonService.payOrderService.updateStatus4Success(payOrder.getPayOrderId());
                if (updatePayOrderRows != 1) {
                    _log.error("{}更新支付状态失败,将payOrderId={},更新payStatus={}失败", logPrefix, payOrder.getPayOrderId(), PayConstant.PAY_STATUS_SUCCESS);
                    retObj.put(PayConstant.RESPONSE_RESULT, "处理订单失败");
                    return retObj;
                }
                _log.error("{}更新支付状态成功,将payOrderId={},更新payStatus={}成功", logPrefix, payOrder.getPayOrderId(), PayConstant.PAY_STATUS_SUCCESS);
                payOrder.setStatus(PayConstant.PAY_STATUS_SUCCESS);
            }
            // 业务系统后端通知
            baseNotify4MchPay.doNotify(payOrder, true);
            _log.info("====== 完成处理优术支付回调通知 ======");
            respString = YmkjpayConfig.RETURN_VALUE_SUCCESS;
        } catch (Exception e) {
            _log.error(e, logPrefix + "处理异常");
        }
        retObj.put(PayConstant.RESPONSE_RESULT, respString);
        return retObj;
    }

    public static void main(String[] args) {


        // order_id=34, out_order_id=P01201910131022072930000, price=301.0000, realprice=301.0000, type=alipay, paytime=1570976730, extend=, sign=67217CECD2C14FDD25A2651A1D167AF5
        // extend=&order_id=34&out_order_id=P01201910131022072930000&paytime=1570976730&price=301.0000&realprice=301.0000&type=alipaykeyYru8pQ5DoAsW3J6g



        String signTemp = "order_id=34&out_order_id=P01201910131022072930000&paytime=1570976730&price=301.0000&realprice=301.0000&type=alipay&key=Yru8pQ5DoAsW3J6g";

                        //"order_id=34&out_order_id=P01201910131022072930000&paytime=1570976730&price=301.0000&realprice=301.0000&type=alipay&key=Yru8pQ5DoAsW3J6g"
        System.out.println(MD5Util.string2MD5(signTemp).toUpperCase());
        System.out.println("67217CECD2C14FDD25A2651A1D167AF5");



    }


}
